const appPath = `/spa/nba-exchange/`
const outputDir = 'dist/H5CDN/webar/projects' + appPath
const fs = require('fs')
const path = require('path')

const srcFile = path.join(__dirname, outputDir, 'index.html')
const destDir = path.resolve(__dirname, 'dist/H5AR_WEB' + appPath + '/')


// 增加文件夹
function createFolder(dirpath, dirname) {
	if (typeof dirname === "undefined") {
		if (fs.existsSync(dirpath)) {
		} else {
			createFolder(dirpath, path.dirname(dirpath));
		}
	} else {
		if (dirname !== path.dirname(dirpath)) {
			createFolder(dirpath);
			return;
		}
		if (fs.existsSync(dirname)) {
			fs.mkdirSync(dirpath)
		} else {
			createFolder(dirname, path.dirname(dirname));
			fs.mkdirSync(dirpath);
		}
	}
}

// 复制文件
function copyFile(orgfilepath, desdirpath, desfilename) {
    if (fs.existsSync(orgfilepath)) {
		let desfilepath = path.join(desdirpath, desfilename);
		if (!fs.existsSync(desfilepath)) {
			createFolder(desdirpath);
			fs.copyFileSync(orgfilepath, desfilepath);
		} else {
			console.error(Date().toString() + "FolderAndFileOperation_copyFile: des file already existed." + " new path: " + desfilepath.toString());
		}
	} else {
		console.error(Date().toString() + "FolderAndFileOperation_copyFile: org file not existed." + " org path: " + orgfilepath.toString());
	}
}

const destFile = destDir+'/index.html'
const checkDir = fs.existsSync(destFile);
if(checkDir){
	fs.unlinkSync(destFile);
}
copyFile(srcFile, destDir, "index.html");




